package dao.mysql;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import model.FacilityType;
import model.LoginCredentials;
import dao.FacilityTypeDAO;

public class FacilityTypeDAOImpl implements FacilityTypeDAO{
	DataSource dataSource=null;
	Connection con=null;
	PreparedStatement pstmt=null;
	public FacilityTypeDAOImpl(){
		try{
			//dataSource=(DataSource) new InitialContext().lookup("java:comp/env/jdbc/fbs");
			//con=dataSource.getConnection();
			con=ConnectionProperties.getConnection();
			}
			catch(Exception ee){
			System.out.println(ee.getMessage());
			}
	}
	@Override
	public void insertFacilityType(FacilityType facilityType)
			throws SQLException {
		// TODO Auto-generated method stub
		try{
		pstmt=con.prepareStatement("insert into FacilityType values (?,?,?,?)");
		pstmt.setInt(1, facilityType.getFacilityTypeID());
		pstmt.setString(2, facilityType.getFacilityType());
		pstmt.setInt(3, facilityType.getTotalRooms());
		pstmt.setInt(4, facilityType.getAvailableRooms());
		pstmt.executeUpdate();
		}
		catch(SQLException e) {
		
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		}
		
		
	
	@Override
	public void updateFacilityType(FacilityType facilityType)
			throws SQLException {
		// TODO Auto-generated method stub
		try{
		pstmt=con.prepareStatement("update FacilityType set FacilityType=?,TotalRooms=?,AvailableRooms=? where FacilityID=?");
		pstmt.setString(1, facilityType.getFacilityType());
		pstmt.setInt(2, facilityType.getTotalRooms());
		pstmt.setInt(3, facilityType.getAvailableRooms());
		pstmt.setInt(4, facilityType.getFacilityTypeID());
		pstmt.executeUpdate();
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		
	}
	@Override
	public void deleteFacilityType(int FacilityTypeID) throws SQLException {
		// TODO Auto-generated method stub
		try{
			pstmt=con.prepareStatement("delete from FacilityType where FacilityID=?");
			pstmt.setInt(1, FacilityTypeID);
			pstmt.executeUpdate();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		
		}
		
	
	@Override
	public FacilityType findFacilityType(int FacilityID) throws SQLException {
		// TODO Auto-generated method stub
		FacilityType ft=null;
		try {
			pstmt=con.prepareStatement("select * from FacilityType where FacilityID=?");
			pstmt.setInt(1, FacilityID);
			ResultSet rs=pstmt.executeQuery();
			
			if(rs.next()){
				ft=new FacilityType();
				ft.setFacilityTypeID(rs.getInt("FacilityTypeID"));
				ft.setFacilityType(rs.getString("FacilityType"));
				ft.setTotalRooms(rs.getInt("TotalRooms"));
				ft.setAvailableRooms(rs.getInt("AvailableRooms"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		return ft;

	}
	@Override
	public ArrayList<FacilityType> findAllFacilityType() throws SQLException {
		// TODO Auto-generated method stub
		ArrayList<FacilityType> ftArrayList=new ArrayList<FacilityType>();
		try {
			pstmt=con.prepareStatement("select * from FacilityType");
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				FacilityType ft=new FacilityType();
				ft.setFacilityTypeID(rs.getInt("FacilityTypeID"));
				ft.setFacilityType(rs.getString("FacilityType"));
				ft.setTotalRooms(rs.getInt("TotalRooms"));
				ft.setAvailableRooms(rs.getInt("AvailableRooms"));
				ftArrayList.add(ft);
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		return  ftArrayList;
	}
	@Override
	public ArrayList<String> findAllFacilityTypeName() {
		// TODO Auto-generated method stub
		ArrayList<String> ftArrayList=new ArrayList<String>();
		try {
			pstmt=con.prepareStatement("select FacilityType from FacilityType");
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				ftArrayList.add(rs.getString("FacilityType"));
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			try {
				pstmt.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return  ftArrayList;
	}



}
